package com.example.helper2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;

public class rumourCardAdapter extends BaseAdapter{
    private List<RumourCardInfo> rlist;
    private Context rcontext;

    public rumourCardAdapter(List<RumourCardInfo> rlist, Context rcontext){
        this.rlist = rlist;
        this.rcontext = rcontext;
    }

    public int getCount(){
        return rlist.size();
    }

    public Object getItem(int position){
        return rlist.get(position);
    }
    public long getItemId(int position){
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent){
        viewholder vh = null;
        if (convertView == null){
            vh = new viewholder();
            convertView = LayoutInflater.from(rcontext).inflate(R.layout.rumour_card,null);
            vh.rCardTitle = (TextView)convertView.findViewById(R.id.rumour_title);
            vh.rCardDate = (TextView)convertView.findViewById(R.id.rumour_date);
            vh.rCardAbstract = (TextView)convertView.findViewById(R.id.rumour_abstract);
            vh.rbutton = (Button)convertView.findViewById(R.id.details);
            convertView.setTag(vh);
        }else{
            vh = (viewholder)convertView.getTag();
        }

        vh.rCardDate.setText(rlist.get(position).getDate());
        vh.rCardTitle.setText(rlist.get(position).getTitle());
        if(rlist.get(position).getAbstractText().length() > 25){
            String rcontentTemp = rlist.get(position).getAbstractText().substring(0,24) + "...";
            rlist.get(position).setAbstractText(rcontentTemp);
        }
        vh.rCardAbstract.setText(rlist.get(position).getAbstractText());
        vh.rbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rOnItemShowListener.onShowClick(position);
            }
        });
        return convertView;
    }

    // 监听按钮点击事件，展示对话框
    public interface onItemShowDialogListener{
        void onShowClick(int position);
    }

    private onItemShowDialogListener rOnItemShowListener;

    public void setOnItemShowDialogClickListener(onItemShowDialogListener rOnItemShowListener){
        this.rOnItemShowListener = rOnItemShowListener;
    }

    public class viewholder{
        TextView rCardDate;
        TextView rCardTitle;
        TextView rCardAbstract;
        Button rbutton;
    }
}
